package com.da.lon.wang.xlg.imgupload;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.util.WebViewUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.DisplayImageOptions.Builder;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.List;

public class ChooseGridAdapter extends BaseAdapter {

    public final float DENSITY = Resources.getSystem().getDisplayMetrics().density;
    private LayoutInflater inflater;
    private String dir;
    private List<ImageInfo> infos;
    private Context context;
    private int screenWidth;
    private LayoutParams params;
    private DisplayImageOptions options;

    public ChooseGridAdapter(Context context, List<ImageInfo> infos) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.infos = infos;

        DisplayMetrics dm = new DisplayMetrics();

        ((Activity) context).getWindowManager().getDefaultDisplay()
                .getMetrics(dm);

        screenWidth = dm.widthPixels;
        params = new LayoutParams((screenWidth - dipToPixel(3) * 3) / 4,
                (screenWidth - dipToPixel(3) * 3) / 4);
        Builder build = new DisplayImageOptions.Builder();
        build.cacheInMemory();
        build.imageScaleType(ImageScaleType.EXACTLY_STRETCHED);
        build.bitmapConfig(Bitmap.Config.RGB_565);
        build.showImageForEmptyUri(R.drawable.xlg_picture_default);
        build.showImageOnFail(R.drawable.xlg_picture_default);
        options = build.build();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return infos != null ? infos.size() + 1 : 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return infos != null && infos.size() > position ? infos.get(position)
                : null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        try {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.xlg_image_grid_item, null);
                holder = new ViewHolder();
                holder.imageView = (ImageView) convertView.findViewById(R.id.image);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.imageView.setLayoutParams(params);
            final ImageView temp = holder.imageView;
            holder.imageView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    temp.setColorFilter(Color.parseColor("#ff0000"));

                }
            });

            if (null != infos && position == infos.size()) {
                holder.imageView.setImageResource(R.drawable.quick_cover_add);
                holder.imageView.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        WebViewUtil.showImageChooseDialog(context);
                    }
                });
            } else {
                ImageLoader.getInstance().displayImage(
                        "file://" + infos.get(position).path, holder.imageView,
                        options);
                holder.imageView.setOnClickListener(null);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        // bitmapUtils.display(holder.imageView, dir + "/" +
        // imgs.get(position));
        return convertView;
    }

    public int dipToPixel(int dip) {
        return (int) (dip * DENSITY + 0.5f);
    }

    public float pixelToDip(float px) {
        return (px - 0.5f) / DENSITY;
    }

    private class ViewHolder {

        ImageView imageView;

    }

}
